angular.module('starter')
.controller('payCtrl', function($scope, $stateParams,$timeout, meilvFac, $state, cfg, $location) {
  $scope.order_id = $stateParams.order_id;

  $scope.getOrderDetail = function(){
    meilvFac.order.orderget({
      order_id: $scope.order_id,
      type: -1
    },function(data){
      $scope.order = data.info[0];
      //$scope.order.project='1'一日游'2'门票
    },function(data){
      alert(data.data.info);
    });
  };
  $scope.getOrderDetail();

  $scope.pay = function(){
    meilvFac.order.wxpay({
      order_id: $scope.order_id
    },function(response){
      var res = response;
      if(res.info){
        alert(res.info);
        return;
      }
      // console.log(res.redirect);
      var jsApiCall = function(data){
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest',
          data,
          function(res){
            WeixinJSBridge.log(res.err_msg);
            // alert(res.err_code+res.err_desc+res.err_msg);
            if (res.err_msg == 'get_brand_wcpay_request:ok') {
              alert("支付成功!");
              $location.url('orderstate/2');
              $scope.$apply();
            }else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
              alert("已取消支付!");
            }else if (res.err_msg == 'get_brand_wcpay_request:fail') {
              alert("支付失败!");
            }else{
              alert('支付失败'+res.err_msg);
            };
          }
        );
      }
      jsApiCall(res);
    },function(data){
      alert(data.data.info);
    });
  };



})